package org.ysu.shoeShopping.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.ysu.shoeShopping.dto.OrderRequest;
import org.ysu.shoeShopping.entity.Orders;
import org.ysu.shoeShopping.service.OrdersService;
import org.ysu.shoeShopping.service.CartService;
import org.ysu.shoeShopping.tools.Result;
import java.util.List;

import javax.annotation.Resource;

/**
 * 订单表(Orders)表控制层
 *
 * @author makejava
 * @since 2025-09-13 17:10:55
 */
@RestController
@RequestMapping("order")
@CrossOrigin(origins = "*")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;

    @Resource
    private CartService cartService;

    /**
     * 分页查询
     *
     * @param orders 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Orders>> queryByPage(Orders orders, PageRequest pageRequest) {
        return ResponseEntity.ok(this.ordersService.queryByPage(orders, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Orders> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.ordersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param orders 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Orders> add(Orders orders) {
        return ResponseEntity.ok(this.ordersService.insert(orders));
    }

    /**
     * 编辑数据
     *
     * @param orders 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Orders> edit(Orders orders) {
        return ResponseEntity.ok(this.ordersService.update(orders));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.ordersService.deleteById(id));
    }

    /**
     * 根据用户ID查询订单列表
     *
     * @param id 用户ID
     * @return 订单详情列表
     */
    @GetMapping("findByUserId/{id}")
    public Result<List<Orders>> findByUserId(@PathVariable("id") Integer id) {
        try {
            List<Orders> orders = this.ordersService.findByUserId(id);
            return Result.success(orders);
        } catch (Exception e) {
            return Result.fail("500", "查询订单失败: " + e.getMessage());
        }
    }
    @GetMapping("findAll")
    public Result<List<Orders>>findCartOrdersAll()
    {
        try {
            List<Orders>orders=this.ordersService.findCartOrdersAll();
            return Result.success(orders);
        }catch (Exception e)
        {
            return Result.fail("500","查询失败"+e.getMessage());
        }
    }

    /**
     * 提交订单
     *
     * @param orderRequest 订单信息
     * @return 提交结果
     */
    @PostMapping("/submit")
    public Result<Orders> submitOrderMessage(@RequestBody OrderRequest orderRequest) {
        try {
            // 验证必需的参数
            if (orderRequest.getUid() == null || orderRequest.getOrderItems() == null ||
                orderRequest.getOrderItems().isEmpty()) {
                return Result.fail("400", "缺少必需的参数");
            }

            // 创建订单
            Orders order = ordersService.createOrder(orderRequest);

            if (order != null) {
                return Result.success(order);
            } else {
                return Result.fail("500", "订单创建失败");
            }
        } catch (Exception e) {
            return Result.fail("500", "提交订单失败: " + e.getMessage());
        }
    }

    /**
     * 根据订单ID删除订单
     *
     * @param id 订单ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteOrder(@PathVariable Integer id) {
        try {
            boolean success = ordersService.deleteById(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.fail("404", "未找到该订单");
            }
        } catch (Exception e) {
            return Result.fail("500", "删除失败: " + e.getMessage());
        }
    }

}

